/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Source;
import net.jxta.document.*;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.logging.Logger;
import net.jxta.protocol.PipeAdvertisement;

/**
 *
 * @author Miguel
 */
public class AdvertisementAskForFile extends PipeAdvertisement implements Comparable, Cloneable, Serializable {
    private ID id = ID.nullID;
    private String namePeer;
    private String valor;
    private String nombreArchivo;
    private String tipoAnuncio;
    private String fechaPeticion;
    private final static Logger LOG = Logger.getLogger(AdvertisementAskForFile.class.getName());
    
    private final static String idTag = "ID_Peer";
    private final static String namePeerTag = "nombre_Peer";
    private final static String valorTag = "Valor_Repuesta";
    private final static String nombreArchivoTag = "nombre_Archivo";
    private final static String tipoAnuncioTag = "tipo_De_Anuncio";
    private final static String fechaCreacionTag = "fecha_Creacion";
    /**
     * Indexable fields. Advertisements must define the indexables, in order
     * to properly index and retrieve these advertisements locally and on the
     * network
     */
    private final static String[] fields = {idTag,namePeerTag, valorTag, nombreArchivoTag, tipoAnuncioTag,fechaCreacionTag};

    /**
     * Default Constructor
     */
    public AdvertisementAskForFile() {
        super();
    }

    /**
     * Construct from a StructuredDocument
     *
     * @param root Root element
     */
    public AdvertisementAskForFile(Element root) {
        TextElement doc = (TextElement) root;
        if (!getAdvertisementType().equals(doc.getName())) {
            throw new IllegalArgumentException("Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
        }
        initialize(doc);
    }

    /**
     * Construct a doc from InputStream
     *
     * @param stream the underlying input stream.
     * @throws IOException if an I/O error occurs.
     */
    public AdvertisementAskForFile(InputStream stream) throws IOException {
        StructuredTextDocument doc = (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument(MimeMediaType.XMLUTF8, stream);
        initialize(doc);
    }


    public void setFechaCreacion(String f) {
        this.fechaPeticion = f;
    }

    /**
     * sets the unique id
     *
     * @param id The id
     */
    public void setID(ID id) {
        this.id = (id == null ? null : id);
    }
    /**
     * sets the unique id
     *
     * @param id The id
     */
    public void setNamePeer(String name) {
        namePeer = (name == null ? null : name);
    }

    /**
     * Sets the iP attribute of the AdvertisementTutorial object
     *
     * @param valor The new iP value
     */
    public void setValor(String v) {
        this.valor = v;
    }

    /**
     * Sets the oSName attribute of the AdvertisementTutorial object
     *
     * @param osname The new oSName value
     */
    public void setNombreArchivo(String nombreArchivo) {
        this.nombreArchivo = nombreArchivo;
    }
    public void setTipoAnuncio(String type)
    {
         tipoAnuncio= type;
    }


    /**
     * {@inheritDoc}
     *
     * @param asMimeType Document encoding
     * @return The document value
     */
    @Override
    public Document getDocument(MimeMediaType asMimeType) {
        StructuredDocument adv = StructuredDocumentFactory.newStructuredDocument(asMimeType,
                getAdvertisementType());
        if (adv instanceof Attributable) {
            ((Attributable) adv).addAttribute("xmlns:jxta", "http://jxta.org");
        }
        Element e;
        e = adv.createElement(idTag, getID().toString());
        adv.appendChild(e);
        e = adv.createElement(namePeerTag, getNamePeer().trim());
        adv.appendChild(e);
        e = adv.createElement(nombreArchivoTag, getNombreArchivo().trim());
        adv.appendChild(e);
        e = adv.createElement(tipoAnuncioTag, getTipoAnuncio().trim());
        adv.appendChild(e);
        e = adv.createElement(valorTag, getValor().trim());
        adv.appendChild(e);
        e = adv.createElement(fechaCreacionTag, getfechaPeticion().trim());
        adv.appendChild(e);
        return adv;
    }

    /**
     * returns the id of the device
     *
     * @return ID the device id
     */
    @Override
    public ID getID() {
        return (id == null ? null : id);
    }
    public String getNamePeer() {
       return (namePeer==null)?"N/A":namePeer;
    }

    /**
     * Gets the IP attribute of the AdvertisementTutorial object
     *
     * @return The IP value
     */
    public String getValor() {
        return (valor==null)?"N/A":valor;
    }

    public String getfechaPeticion() {
        return (fechaPeticion==null)?"N/A":fechaPeticion;
    }
    public String getNombreArchivo() {
        return nombreArchivo;
    }
    public String getTipoAnuncio()
    {
        return (tipoAnuncio==null)?"N/A":tipoAnuncio;
    }

    /**
     * Process an individual element from the document.
     *
     * @param elem the element to be processed.
     * @return true if the element was recognized, otherwise false.
     */
    protected boolean handleElement(TextElement elem) {
        if (elem.getName().equals(idTag)) {
            try {
                URI id = new URI(elem.getTextValue());
                setID(IDFactory.fromURI(id));
            } catch (URISyntaxException badID) {
                throw new IllegalArgumentException("unknown ID format in advertisement: " + elem.getTextValue());
            } catch (ClassCastException badID) {
                throw new IllegalArgumentException("Id is not a known id type: " + elem.getTextValue());
            }
            return true;
        }
        if (elem.getName().equals(namePeerTag)) {
            setNamePeer(elem.getTextValue());
            return true;
        }
        if (elem.getName().equals(nombreArchivoTag)) {
            setNombreArchivo(elem.getTextValue());
            return true;
        }
        if (elem.getName().equals(valorTag)) {
            setValor(elem.getTextValue());
            return true;
        }
        if (elem.getName().equals(fechaCreacionTag)) {
            setFechaCreacion(elem.getTextValue());
            return true;
        }
        if (elem.getName().equals(tipoAnuncioTag)) {
            setTipoAnuncio(elem.getTextValue());
            return true;
        }
        return false;
    }

    /**
     * Intialize a System advertisement from a portion of a structured document.
     *
     * @param root document root
     */
    protected void initialize(Element root) {
        if (!TextElement.class.isInstance(root)) {
            throw new IllegalArgumentException(getClass().getName() + " only supports TextElement");
        }
        TextElement doc = (TextElement) root;
        if (!doc.getName().equals(getAdvertisementType())) {
            throw new IllegalArgumentException("Could not construct : " + getClass().getName() + "from doc containing a " + doc.getName());
        }
        Enumeration elements = doc.getChildren();
        while (elements.hasMoreElements()) {
            TextElement elem = (TextElement) elements.nextElement();
            if (!handleElement(elem)) {
                LOG.warning("Unhandleded element \'" + elem.getName() + "\' in " + doc.getName());
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String[] getIndexFields() {
        return fields;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof AdvertisementAskForFile) {
            AdvertisementAskForFile adv = (AdvertisementAskForFile) obj;
            return getID().equals(adv.getID());
        }
        return false;
    }

    @Override
    public String toString() {
        return "peticion";
    }


    /**
     * {@inheritDoc}
     */
    public int compareTo(Object other) {
        return getID().toString().compareTo(other.toString());
    }

    /**
     * All messages have a type (in xml this is &#0033;doctype) which
     * identifies the message
     *
     * @return String "jxta:AdvertisementTutorial"
     */
    public static String getAdvertisementType() {
        return "jxta:AdvertisementAskForFile";
    }

    /**
     * Instantiator
     */
    public static class Instantiator implements AdvertisementFactory.Instantiator {

        /**
         * Returns the identifying type of this Advertisement.
         *
         * @return String the type of advertisement
         */
        public String getAdvertisementType() {
            return AdvertisementAskForFile.getAdvertisementType();
        }

        /**
         * Constructs an instance of <CODE>Advertisement</CODE> matching the
         * type specified by the <CODE>advertisementType</CODE> parameter.
         *
         * @return The instance of <CODE>Advertisement</CODE> or null if it
         * could not be created.
         */
        public Advertisement newInstance() {
            return new AdvertisementAskForFile();
        }

        /**
         * Constructs an instance of <CODE>Advertisement</CODE> matching the
         * type specified by the <CODE>advertisementType</CODE> parameter.
         *
         * @param root Specifies a portion of a StructuredDocument which will
         * be converted into an Advertisement.
         * @return The instance of <CODE>Advertisement</CODE> or null if it
         * could not be created.
         */
        public Advertisement newInstance(net.jxta.document.Element root) {
            return new AdvertisementAskForFile(root);
        }
    }

}